#include <iostream>
#include <queue>
#include <string.h>

using namespace std;

int maze[8][8] =
{
     { 1, 0, 1, 1, 1, 1, 1, 1 },
     { 1, 0, 0, 0, 0, 0, 0, 1 },
     { 1, 0, 1, 1, 1, 1, 0, 1 },
     { 1, 0, 0, 0, 0, 1, 0, 0 },
     { 1, 1, 1, 1, 0, 0, 1, 1 },
     { 1, 1, 1, 1, 1, 1, 1, 1 },
     { 1, 1, 0, 0, 0, 0, 1, 1 },
     { 1, 1, 0, 1, 1, 1, 1, 1 },
};
const int inf = 1000;
int dis[8][8];
int dir[4][2] =
{
	{-1, 0},
	{1, 0},
	{0, -1},
	{0, 1},
};

struct pos
{
	int x;
	int y;
	pos(int ix, int iy)
		:x(ix)
		 ,y(iy)
	{}
};

bool valid(int x, int y)
{
	if (x >= 0 && x <= 7 && y >=0 && y <= 7
			&& maze[x][y] == 1
			&& dis[x][y] == inf)
	{
		return true;
	}

	return false;
}

void bfs()
{
	for (int i = 0; i < 8; ++i)
	{
		for (int j = 0; j < 8; ++j)
		{
			dis[i][j] = inf;
		}
	}
	std::queue<pos> QuePos;
	QuePos.push(pos(0,0));
	dis[0][0] = 0;
	while (0 != QuePos.size())
	{
		pos tmp = QuePos.front();
		QuePos.pop();
		int x = tmp.x;
		int y = tmp.y;
		if (x >= 7 && y >= 7)
		{
			break;
		}
		for (int i = 0; i < 4; ++i)
		{
			int nx = x + dir[i][0];
			int ny = y + dir[i][1];
			if (valid(nx, ny))
			{
				QuePos.push(pos(nx, ny));
				dis[nx][ny] = dis[x][y] + 1;
			}
		}
	}
}

int main()
{
	bfs();
	for (int i = 0; i < 8; ++i)
	{
		for (int j = 0; j < 8; ++j)
		{
			cout << dis[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}
